package com.car.service.impl.TReturn;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.car.dao.TReturn.TReturnDao;
import com.car.entity.TReturn;
import com.car.service.TReturn.TReturnService;
import com.car.util.ResultMsg;
import com.car.util.SnowFlakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.util.ManifestEntryVerifier;

@Service
public class TReturnServiceImpl implements TReturnService {

    @Autowired
    TReturnDao tReturnDao;

    @Autowired
    SnowFlakeIdWorker snowFlakeIdWorker;

    public ResultMsg InsertTReturn(TReturn tReturn) {
        ResultMsg resultMsg = new ResultMsg();
        tReturn.setReturnId(String.valueOf("R" + snowFlakeIdWorker.nextId()));
        int i = tReturnDao.insert(tReturn);
        if (i > 0) {
            resultMsg.setMsg("回退金额添加成功");
            resultMsg.setFlag(1);
            resultMsg.setEntityObj(tReturn);
        } else {
            resultMsg.setMsg("回退金额添加失败");
            resultMsg.setFlag(0);
        }
        return resultMsg;
    }

    public ResultMsg SelectOneQuery(TReturn tReturn) {
        LambdaQueryWrapper<TReturn> wrapper = new LambdaQueryWrapper<TReturn>();
        wrapper.eq(TReturn::getReturnId, tReturn.getReturnId());
        TReturn tReturn1 = tReturnDao.selectOne(wrapper);
        ResultMsg resultMsg = new ResultMsg();
        if (tReturn1 != null) {
            resultMsg.setFlag(1);
            resultMsg.setEntityObj(tReturn1);
        } else {
            resultMsg.setFlag(0);
        }
        return resultMsg;
    }

    public ResultMsg SelectOneByOrderIdQuery(TReturn tReturn) {
        LambdaQueryWrapper<TReturn> wrapper = new LambdaQueryWrapper<TReturn>();
        wrapper.eq(TReturn::getOrderId, tReturn.getOrderId());
        TReturn tReturn1 = tReturnDao.selectOne(wrapper);
        ResultMsg resultMsg = new ResultMsg();
        if (tReturn1 != null) {
            resultMsg.setFlag(1);
            resultMsg.setEntityObj(tReturn1);
        } else {
            resultMsg.setFlag(0);
        }
        return resultMsg;
    }

    public ResultMsg UpdateTReturnflag(TReturn tReturn) {
        ResultMsg resultMsg = new ResultMsg();
        LambdaUpdateWrapper<TReturn> wrapper = new LambdaUpdateWrapper<TReturn>();
        wrapper.eq(TReturn::getReturnId,tReturn.getReturnId());
        wrapper.set(TReturn::getReturnFlag,"1");
        int i = tReturnDao.update(null, wrapper);
        if(i>0){
            resultMsg.setFlag(1);
            resultMsg.setMsg("修改退款表成功");
        }else{
            resultMsg.setFlag(0);
            resultMsg.setMsg("修改退款表失败");
        }
        return resultMsg;
    }
}
